<?php
	namespace Api\Controller;
	use Think\Controller;
	header("Content-Type:text/html;charset=utf-8");
	class SearchController extends Controller
	{
		public function searchInfo($type=null, $searchstring=null, $count=null)
		{
			if ($searchstring==null || $type==null) 
			{
				$array['result'] = 10101;
				$array['message'] = "搜索信息获取失败";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$where['keyword'] = $searchstring;
				$res = M("Antistop") -> where($where) ->find();
				if ($res != null) 
				{
					M("Antistop") -> where("nid={$res['nid']}") -> setInc("clicktimes", 1);
				}
				else
				{
					$string['keyword'] = $searchstring;
					M("Antistop") -> where("nid = {$res['nid']}") -> add($string);
				} 
				if ($type == 1) 
				{
					
					//搜索通告
					$condition['annunciate_r'] = array('like',"%$searchstring%");
					$searchResult = M("Annunciate")-> field("id,annunciate_r,address,applystime") -> where($condition) -> select();
					//var_dump($searchResult);
					$count = count($searchResult);
					foreach ($searchResult as $key=>$value)
					{
						$data = array(
								'id'=>$value['id'],
								'title'=>$value['annunciate_r'],
								'address'=>$value['address'],
								'applystime'=>$value['applystime']
						);
						$searchResult[$key] = $data;						
					}
					if ($searchResult != null)
					{
						$array['result'] = 0;
						$array['message'] = "请求数据成功！";
						$array['annunciate'] = $searchResult;
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						$array['result'] = 10102;
						$array['message'] = "请求数据失败！";
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
				
				}
				elseif ($type == 2)
				{
					if ($count == null) 
					{
						//搜索艺人
						
						$condition['nickname'] = array('like',"%$searchstring%");
						$searchUser = M("User") -> field("uid,nickname,rmtype") ->where($condition) -> order("rmtype desc,dtttype desc") ->select();
						$condition1['tagname'] = array('like',"%$searchstring%");
						
						
						$searchUserinfo = M("Userinfo") -> field("uid,tagname") -> where($condition1) -> select();
						
						foreach ($searchUser as $key=>$value)
						{
							foreach ($searchUserinfo as $k=>$v)
							{
								if ($value['uid']==$v['uid']) unset($searchUserinfo[$k]);
							}
						}
						
						foreach ($searchUser as $key=>$value)
						{
							if ($value['rmtype'] == 0) 
							{
								$uid1[$key] = $value['uid'];
							}
							 
						}
						
						foreach ($uid1 as $key=>$value)
						{
							$userinfo[$key] = M("User") -> field("uid,nickname,dtttype") -> where("uid={$value}") ->find();
							$userinfo[$key]["headimg"] = "";
							$userinfo[$key]["tagname"] = array();
						}
						
						$idinfo = array_merge($searchUserinfo, $searchUser);
						foreach ($idinfo as $key=>$value)
						{
					     	$uid[$key] = $value['uid'];
							
						}
						$uid2 = array_diff($uid,$uid1);
						
						foreach ($uid2 as $k=>$v)
						{
							
							$info = M("User") -> join("__USERINFO__ on __USER__.uid=__USERINFO__.uid") -> field("think_user.uid,dtttype,nickname,tagname,headimg") -> where("think_user.uid={$v}") -> find();
							$tagname = explode("-", $info['tagname']);
							unset($value['tagname']);
							$info["tagname"] = $tagname;
							$userinfo1[$k] = $info;
						} 
						
						$Userinfo = array_merge($userinfo1, $userinfo);
						$count = count($Userinfo);
						if ($Userinfo != null)
						{
							$array['result'] = 0;
							$array['message'] = "请求数据成功！";
							$array['userinfo'] = $Userinfo;
							echo json_encode($array, JSON_UNESCAPED_SLASHES);
							exit;
						}
						else
						{
							$array['result'] = 10103;
							$array['message'] = "请求数据失败！";
							echo json_encode($array, JSON_UNESCAPED_SLASHES);
							exit;
						}
					}
					else
					{
						$condition['nickname'] = array('like',"%$searchstring%");
						$uid3 = M("User") ->where($condition) -> order("regtime desc") ->getField("uid",true);
						$condition1['tagname'] = array('like',"%$searchstring%");

						$uid4 = M("Userinfo") -> where($condition1) -> getField("uid",true);
						
						$uid5 = array_unique(array_merge($uid3,$uid4));
						
						$count1 = count($uid5);
						
						$condition2 = array("in", $uid5);
						$userinfo3 = M("User") -> field("uid,nickname,rmtype") -> where($where) -> order("regtime desc") -> select();
						
						if ($count1 > $count)
						{
							$userinfo2 = array_slice($userinfo3, -($count1-$count));
							foreach ($userinfo2 as $key=>$value)
							{
								$uid6[$key] = $value["uid"];
							}
							if ($userinfo2["rmtype"] == 1) 
							{
								$con["uid"] = $userinfo2["uid"];
								
								$info = M("User") -> join("__USERINFO__ on __USER__.uid=__USERINFO__.uid") -> field("think_user.uid,dtttype,nickname,tagname,headimg") -> where("think_user.uid={$v}") -> find();
								$tagname = explode("-", $info['tagname']);
								unset($value['tagname']);
								$info["tagname"] = $tagname;
								$userinfo1[$k] = $info;
							}
							//var_dump($uid6);
							if ($userinfo2 != null)
							{
								
							}
						}
						
						
						foreach ($uid1 as $key=>$value)
						{
							$userinfo[$key] = M("User") -> field("uid,nickname,dtttype") -> where("uid={$value}") ->find();
							$userinfo[$key]["headimg"] = "";
							$userinfo[$key]["tagname"] = array();
						}
						
						$idinfo = array_merge($searchUserinfo, $searchUser);
						foreach ($idinfo as $key=>$value)
						{
							$uid[$key] = $value['uid'];
								
						}
						$uid2 = array_diff($uid,$uid1);
						
						foreach ($uid2 as $k=>$v)
						{
								
							$info = M("User") -> join("__USERINFO__ on __USER__.uid=__USERINFO__.uid") -> field("think_user.uid,dtttype,nickname,tagname,headimg") -> where("think_user.uid={$v}") -> find();
							$tagname = explode("-", $info['tagname']);
							unset($value['tagname']);
							$info["tagname"] = $tagname;
							$userinfo1[$k] = $info;
						}
						
						$Userinfo = array_merge($userinfo1, $userinfo);
						$count1 = count($Userinfo);
						
					}
				}
				elseif ($type == 3)
				{
					//搜索需求
					$condition = array(
							'dmtitle'=>array('like',"%$searchstring%"),
							'tagname'=>array('like',"%$searchstring%"),
							'_logic'=>'OR',
							/* 'dmcontent'=>array('like',"%$searchstring%") */
					);
					$searhDemand = M("Demand") -> field("dmid,dmtitle,tagname,address,applystime") -> where($condition) ->select();
					foreach ($searhDemand as $key=>$value)
					{
						$tagname = explode("-", $value['tagname']);
						unset($value['tagname']);
						$value["tagname"] = $tagname;
						$searhDemand[$key] = $value;
					}
					
					
					//var_dump($searhDemand);
					if ($searhDemand != null)
					{
						$array['result'] = 0;
						$array['message'] = "请求数据成功！";
						$array['demandinfo'] = $searhDemand;
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						$array['result'] = 10104;
						$array['message'] = "请求数据失败！";
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
				elseif ($type == 4)
				{
					//搜索机构
					$condition = array(
							'intro'=>array('like', "%$searchstring%"),
							'otionname'=>array('like', "%$searchstring%"),
							'_logic'=>'OR',
					);
					$searchOtion = M("Otion") -> field("id,otionname,intro,logo") -> where($condition) -> select();
					
					
					foreach ($searchOtion as $key=>$value)
					{
						$data = array();
						$data = array(
								'id'=>$value['id'],
								'otionname'=>$value['otionname'],
								'logo'=>"http://oc169kdqd.bkt.clouddn.com/".$value['logo'],
								'intro'=>$value['intro']
						);
						$searchOtion[$key] = $data;
					}
					//var_dump($searchOtion);
					if ($searchOtion != null)
					{
						$array['result'] = 0;
						$array['message'] = "请求数据成功！";
						$array['otioninfo'] = $searchOtion;
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						$array['result'] = 10105;
						$array['message'] = "请求数据失败！";
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
				}
				elseif ($type == 5)
				{
					//搜索活动
					$condition = array(
							'actitle'=>array('like', "%$searchstring%")
					);
					$searActivity = M("Activity") -> field("actitle,stoptime,url") -> where($condition) -> select();
					
					foreach ($searActivity as $key=>$value)
					{
						$data = array();
						
						if (time()-strtotime($value["stoptime"])<0)
						{
							$value["status"] = "未结束";	
						}
						else
						{
							$value["status"] = "已结束";
						}
						
						
						$data = array(
								'actitle'=>$value['actitle'],
								'url'=>$value['url'],
								'status'=>$value['status']
						);
						$searActivity[$key] = $data;
						
					}
					//var_dump($searActivity);
					
					if ($searActivity != null)
					{
						$array['result'] = 0;
						$array['message'] = "请求数据成功！";
						$array['activityinfo'] = $searActivity;
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
					else
					{
						$array['result'] = 10106;
						$array['message'] = "请求数据失败！";
						echo json_encode($array, JSON_UNESCAPED_SLASHES);
						exit;
					}
					
				}
			}
		}
		/*
		 *热门搜索 
		 *   
		 *   
		 */
		public function hotSearch()
		{
			$hotstring = M("Antistop") -> field("keyword") -> order("clicktimes desc") ->limit(3) ->select();
			//var_dump($hotstring);
			
			if ($hotstring != null)
			{
				$array['result'] = 0;
				$array['message'] = "请求数据成功！";
				$array['hotstring'] = $hotstring;
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
			else
			{
				$array['result'] = -1;
				$array['message'] = "请求数据失败！";
				echo json_encode($array, JSON_UNESCAPED_SLASHES);
				exit;
			}
		}
	}